package com.shigui.news.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shigui.news.dao.CommentDao;
import com.shigui.news.dao.NewsDao;
import com.shigui.news.enums.ResponseCode;
import com.shigui.news.exception.NewsException;
import com.shigui.news.exception.UserException;
import com.shigui.news.model.Category;
import com.shigui.news.model.News;
import com.shigui.news.model.User;
import com.shigui.news.service.NewsService;
@Service("newsService")
public class NewsServiceImpl implements NewsService {

	@Autowired
	private NewsDao newsDao;

	@Autowired
	private CommentDao commentDao;
	@Override
	public News selectById(Integer newsId) {
		

		

		return newsDao.selectById(newsId);
	}

	@Override
	public List<News> select() {
		return null;
	}

	@Override
	public void insert(User user,News news) {
		
		if(user == null){
			
			throw new UserException(ResponseCode.USER_ALREADY_LOGOUT);
		}

		newsDao.insert(news);
	}

	@Override
	public void update( News news) {
		
		
		
		newsDao.selectById(news.getNewsId());
		
		if(news == null){
			
			throw new NewsException(ResponseCode.NEWS_FIND);
		}
		newsDao.update(news);
		
		
	}
	/**
	 * 事务
	 */

	@Override
	public void delete(User user,News news) {
		
		if(user == null){
			
			throw new UserException(ResponseCode.USER_ALREADY_LOGOUT);
		}
		
		if(news == null){
			
			throw new NewsException(ResponseCode.NEWS_FIND);
		}
		newsDao.delete(news);
		commentDao.delete(news.getNewsId());
		
		
	}


	@Override
	public List<News> getNewsList( Category category, Integer page, Integer count) {
		
		
		
		return newsDao.getNewsList(category, page, count);
	}

	@Override
	public Integer getCountNews( Category category) {
		
		
		return newsDao.countNewsList(category);
	}

	@Override
	public void updateNum(News news) {
		
		newsDao.updateNum(news);
		
	}
	

}
